package com.app.micaihu.view.main.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

import com.app.micaihu.R;
import com.blankj.utilcode.util.ColorUtils;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshKernel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;
import com.scwang.smart.refresh.layout.util.SmartUtil;

import static com.blankj.utilcode.util.StringUtils.getString;

public class ClassHeader extends LinearLayout implements RefreshHeader {

    private TextView mHeaderText;//标题文本
    private ImageView mProgressView;//刷新动画视图
    private Animation operatingAnim;

    public ClassHeader(Context context) {
        super(context);
        initView(context);
    }

    public ClassHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initView(context);
    }

    public ClassHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initView(context);
    }

    private void initView(Context context) {
        operatingAnim = AnimationUtils.loadAnimation(getContext(), R.anim.rotate_360);
        LinearInterpolator lin = new LinearInterpolator();
        operatingAnim.setInterpolator(lin);
        setGravity(Gravity.CENTER);
        mHeaderText = new TextView(context);
        mHeaderText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 13);
        mHeaderText.setTextColor(ColorUtils.getColor(R.color.color888));
        mProgressView = new ImageView(context);
        mProgressView.setImageResource(R.drawable.infor_loading);
        addView(mProgressView, SmartUtil.dp2px(30), SmartUtil.dp2px(30));
        addView(new View(context), SmartUtil.dp2px(10), SmartUtil.dp2px(20));
        addView(mHeaderText, SmartUtil.dp2px(100), LayoutParams.WRAP_CONTENT);
        setMinimumHeight(SmartUtil.dp2px(60));
    }

    @NonNull
    public View getView() {
        return this;//真实的视图就是自己，不能返回null
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;//指定为平移，不能null
    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int headHeight, int maxDragHeight) {
        mProgressView.startAnimation(operatingAnim);//开始动画
    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        if (success) {
            mHeaderText.setText(getString(R.string.pulltorefresh_loading));
        } else {
            mHeaderText.setText(getString(R.string.pulltorefresh_loading)); // 刷新失败
        }
        mProgressView.clearAnimation();//停止动画
        return 100;//延迟500毫秒之后再弹回
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        switch (newState) {
            case None:
            case PullDownToRefresh:
                mHeaderText.setText(getString(R.string.pulltorefresh_down));
                break;
            case Refreshing:
                mHeaderText.setText(getString(R.string.pulltorefresh_loading_up));
                break;
            case ReleaseToRefresh:
                mHeaderText.setText(getString(R.string.pulltorefresh_ready));
                break;
        }
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int maxDragHeight) {
    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
        mProgressView.setRotation(offset);
    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
    }


    @Override
    public void setPrimaryColors(@ColorInt int... colors) {
//        if (colors.length > 0) {
//            this.setBackgroundColor(colors[0]);
//        }
    }
}